//
//  UIButton+SomeMethodOfButton.m
//  BKJ
//
//  Created by 贝壳家iOS on 15/11/24.
//  Copyright © 2015年 贝壳家iOS. All rights reserved.
//

#import "UIButton+SomeMethodOfButton.h"

@implementation UIButton (SomeMethodOfButton)

+(UIButton *)buttonWithTitle:(NSString *)title{
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  [button setTitle:title forState:UIControlStateNormal];
  [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  return button;
}




- (void)setImage:(UIImage *)image highImage:(UIImage *)highImage selectedImage:(UIImage *)selectedImage{
  if (image != nil) {
    [self setImage:image forState:UIControlStateNormal];
  }
  
  if (highImage != nil) {
    [self setImage:highImage forState:UIControlStateHighlighted];
  }
  
  if (selectedImage != nil) {
    [self setImage:selectedImage forState:UIControlStateSelected];
  }
  
  
}



- (void)dw_titleImageHorizontalAlignmentWithSpace:(float)space;
{
  [self dw_resetEdgeInsets];
  [self setNeedsLayout];
  [self layoutIfNeeded];
  
  CGRect contentRect = [self contentRectForBounds:self.bounds];
  CGSize titleSize = [self titleRectForContentRect:contentRect].size;
  CGSize imageSize = [self imageRectForContentRect:contentRect].size;
  
  [self setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, space)];
  [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -imageSize.width)];
  [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleSize.width+space, 0, 0)];
}

- (void)dw_imageTitleHorizontalAlignmentWithSpace:(float)space;
{
  [self dw_resetEdgeInsets];
  [self setTitleEdgeInsets:UIEdgeInsetsMake(0, space, 0, -space)];
  [self setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, space)];
}

- (void)dw_titleImageVerticalAlignmentWithSpace:(float)space;
{
  [self dw_verticalAlignmentWithTitleTop:YES space:space];
}

- (void)dw_imageTitleVerticalAlignmentWithSpace:(float)space;
{
  [self dw_verticalAlignmentWithTitleTop:NO space:space];
}

- (void)dw_verticalAlignmentWithTitleTop:(BOOL)isTop space:(float)space ;
{
  [self dw_resetEdgeInsets];
  [self setNeedsLayout];
  [self layoutIfNeeded];
  
  CGRect contentRect = [self contentRectForBounds:self.bounds];
  CGSize titleSize = [self titleRectForContentRect:contentRect].size;
  CGSize imageSize = [self imageRectForContentRect:contentRect].size;
  
  float halfWidth = (titleSize.width + imageSize.width)/2;
  float halfHeight = (titleSize.height + imageSize.height)/2;
  
  float topInset = MIN(halfHeight, titleSize.height);
  float leftInset = (titleSize.width - imageSize.width)>0?(titleSize.width - imageSize.width)/2:0;
  float bottomInset = (titleSize.height - imageSize.height)>0?(titleSize.height - imageSize.height)/2:0;
  float rightInset = MIN(halfWidth, titleSize.width);
  
  if (isTop) {
    [self setTitleEdgeInsets:UIEdgeInsetsMake(-titleSize.height-space, - halfWidth, imageSize.height+space, halfWidth)];
    [self setContentEdgeInsets:UIEdgeInsetsMake(topInset+space, leftInset, -bottomInset, -rightInset)];
  } else {
    [self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height+space, - halfWidth, -titleSize.height-space, halfWidth)];
    [self setContentEdgeInsets:UIEdgeInsetsMake(-bottomInset, leftInset, topInset+space, -rightInset)];
  }
}

- (void)dw_resetEdgeInsets
{
  [self setContentEdgeInsets:UIEdgeInsetsZero];
  [self setImageEdgeInsets:UIEdgeInsetsZero];
  [self setTitleEdgeInsets:UIEdgeInsetsZero];
}




@end
